<?php

// autor/i etapy: Matúš Novák
// 29.11.2013

class Model_user extends CI_Model {

    public function add_kolektiv($temp){
        $data = array(
            'id_uzivatela' => $temp,
            'trieda' => $this->input->post('nazov'),
            'pocet' => $this->input->post('pocet'),
            'skola' => $this->input->post('skolaKolektiv'),
            'vek' => $this->input->post('vek'),
            'zameranie' => $this->input->post('zameranie')
        );    
        $query = $this->db->insert('kolektiv',$data);
        if ($query){
            return true;
        } else{
            return false;
        }
    }
    
    public function get_kolektivy(){
        $results = array();
        $this->db->select('trieda');
        $this->db->from('kolektiv');
        $this->db->where('id_uzivatela', $this->session->userdata('id'));

        $query = $this->db->get();
        foreach ($query->result() as $row){
        //TODO
            $results[] = $row->trieda;
        }
        return $results;
    }
    
    public function get_id_kolektivy(){
        $results = array();
        $this->db->select('id');
        $this->db->from('kolektiv');
        $this->db->where('id_uzivatela', $this->session->userdata('id'));
        
        $query = $this->db->get();
        foreach ($query->result() as $row){
            $results[] = $row->id;
        }
        return $results;
    }
    
    public function get_id_rozvrh_udalosti($id){
        $results = array();
        $this->db->select('id_cas_udalosti');
        $this->db->from('rozvrh');
        $this->db->where('id_kolektiv', $id);
        
        $query = $this->db->get();
        foreach ($query->result() as $row){
            $results[] = $row->id_cas_udalosti;
        }
        return $results;
    }
    
    public function logout_all($id_kol,$id_ud){
      $this->db->where('id_kolektiv', $id_kol);
      $this->db->where('id_cas_udalosti', $id_ud);
       if ($this->db->delete('rozvrh'))
          return true;
       else return false;
    }
    
    public function get_id_kolektivu($kolektiv){
        $this->db->where('trieda', $kolektiv);
        $user = $this->db->get('kolektiv');
        if ($user) {
             $row = $user->row();
             $user_check = $row->id;
             return $user_check;
        }  
        return NULL;
    }
    
    public function get_info_kolektiv($kolektiv){
        $this->db->where('trieda', $kolektiv);
        $user = $this->db->get('kolektiv');
        if ($user) {
             $row = $user->row();
             $kolektiv = array(
                  'pocet' => $row->pocet,
                  'skola' => $row->skola,
                  'trieda' => $row->trieda,
                  'vek' => $row->vek,
                  'zameranie' => $row->zameranie
             );
             return $kolektiv;
        }  
        return NULL;
    }
    
    public function get_info_kolektiv_forAdmin($kolektiv){
        $this->db->where('id', $kolektiv);
        $user = $this->db->get('kolektiv');
        if ($user) {
             $row = $user->row();
             $kolektiv = array(
                  'pocet' => $row->pocet,
                  'skola' => $row->skola,
                  'trieda' => $row->trieda,
                  'vek' => $row->vek,
                  'zameranie' => $row->zameranie
             );
             return $kolektiv;
        }  
        return NULL;
    }
    
    public function deleteKolektiv($temp){
       $this->db->where('id_kolektiv', $temp);
       if ($this->db->delete('rozvrh')){
          $this->db->where('id', $temp);
          if ($this->db->delete('kolektiv'))
            return true;
       }
       else return false;
    }
    
    public function change_kolektiv($temp){
        $this->db->where('id', $temp);
        $data = array(
            'id_uzivatela' => $this->session->userdata('id'),
            'trieda' => $this->input->post('nazov'),
            'pocet' => $this->input->post('pocet'),
            'skola' => $this->input->post('skolaKolektiv'),
            'vek' => $this->input->post('vek'),
            'zameranie' => $this->input->post('zameranie')
        );
        if ($this->db->update('kolektiv', $data))
            return true;
        else return false;
    }
    
    public function get_maxLudi($temp){
       $this->db->where('id', $temp);
       $max = $this->db->get('udalost');
       if ($max) {
             $row = $max->row();
             $max_lud = $row->maxludi;
             return $max_lud;
       }
       return NULL;
    }
    
    public function get_zaplnenie($temp){
       $this->db->where('id', $temp);
       $max = $this->db->get('casy_udalosti');
       if ($max) {
             $row = $max->row();
             $zap = $row->zaplnenie;
             return $zap;
       }
       return NULL;
    }
    
    public function get_pocet_kolektiv($temp){
       $this->db->where('id', $temp);
       $max = $this->db->get('kolektiv');
       if ($max) {
             $row = $max->row();
             $poc = $row->pocet;
             return $poc;
       }
       return NULL;
    }
    
    public function get_trvanie($temp){
       $this->db->where('id', $temp);
       $max = $this->db->get('casy_udalosti');
       if ($max) {
             $row = $max->row();
             $poc = $row->trvanie;
             return $poc;
       }
       return NULL;
    }
    
    public function get_cas_od($temp){
       $this->db->where('id', $temp);
       $max = $this->db->get('casy_udalosti');
       if ($max) {
             $row = $max->row();
             $poc = $row->cas_od;
             return $poc;
       }
       return NULL; 
    }
	
	public function get_cas_udalosti($temp){
       $this->db->where('id', $temp);
       $max = $this->db->get('casy_udalosti');
       if ($max) {
             $row = $max->row();
			 $cas = array(
                  'cas_od' => $row->cas_od,
                  'cas_do' => $row->cas_do,
                  'trvanie' => $row->trvanie,
                  'den' => $row->den,
                  'fixna' => $row->fixna
             );
             return $cas;
       }
       return NULL; 
    }
    
    public function update_udalost($temp){
       $this->db->where('id', $temp['id']);
       $data = array(
            'cas_od' => $temp['cas_od'],
            'cas_do' => $temp['cas_do'],
            'zaplnenie' => $temp['zaplnenie'],
            'fixna' => true
        );
        if ($this->db->update('casy_udalosti', $data))
            return true;
        else return false;
    }
    
    public function update_zaplnenie($temp, $lud){
       $this->db->where('id', $temp);
       $data = array(
            'zaplnenie' => $lud
        );
        if ($this->db->update('casy_udalosti', $data))
            return true;
        else return false;
    }
    
    public function prihlas_udalost($udalost,$kolektiv){
         $data = array(
            'id_cas_udalosti' => $udalost,
            'id_kolektiv' => $kolektiv
        );    
        $query = $this->db->insert('rozvrh',$data);
        if ($query){
            return true;
        } else{
            return false;
        }
    }
    
    public function is_not_in($id_kol,$id_udal){
       $this->db->where('id_cas_udalosti', $id_udal);
       $this->db->where('id_kolektiv', $id_kol);
       $max = $this->db->get('rozvrh');
       if ($max) {
             $row = $max->row();
             $kol = $row->id_kolektiv;
             $udal =  $row->id_cas_udalosti;
             if($kol != NULL and $udal != NULL){ return false;
             }else return true;
       }
       return true;
    }
	
	
	public function prekryva_sa($id_kol, $id_termin, $fl_start = -1) {
		/*
		vstup: id kolektivu a id terminu (aj plavajuceho) 
		vystup true ak sa prekryva false ak nieje konflikt
		*/
		$udalost = $this->get_udalost($id_termin);
		if ($fl_start == -1) {
			$udalost->cas_od = $this->StrToMins($udalost->cas_od);
		} else {
			$udalost->cas_od = $fl_start;
		}
		$this->db->select('id_cas_udalosti')
			->from('rozvrh')
			->where('id_kolektiv',$id_kol);
		$query = $this->db->get();
		foreach($query->result() as $row) {
			$temp = $this->get_udalost($row->id_cas_udalosti);
			if ($temp->den != $udalost->den) {
				continue;
			}
			$temp->cas_od = $this->StrToMins($temp->cas_od);
			if (($udalost->cas_od >= $temp->cas_od and 
				$udalost->cas_od  < ($temp->cas_od + $temp->trvanie))) {
				return true;
			}
			if (($temp->cas_od >= $udalost->cas_od) and 
				($temp->cas_od  < ($udalost->cas_od + $udalost->trvanie))) {
				return true;
			}
		}
		return false;
	}
		
	public function StrToMins($s) {
		$tm = strtotime($s);
		return (idate("i",$tm)+ 60 * idate("H",$tm));
	}
	
		
	public function get_udalost($id) {
		$this->db->select('*')
			->from('casy_udalosti')
			->where('id',$id);
		$query = $this->db->get();
		foreach ($query->result() as $row) {
			return $row;
		}
		return null;
	}
}
?>